import QtQuick 2.15
import QtQuick.Controls 2.15
import QtGraphicalEffects 1.15
import './../../../controls'
//下单窗口
Popup {
    id:pop
    width: 800
    height: 600
    modal: true
    anchors.centerIn: parent
    property var pWin: null
    property bool returnGoods: false //标记是否是退货
    property var goodData: null
    property var orderInfo: null
    property string bno: ''
    closePolicy:Popup.NoAutoClose
    background:BorderImage {
        anchors.fill: parent
        anchors.leftMargin: -16
        anchors.rightMargin: -16
        anchors.topMargin: -12
        anchors.bottomMargin: -20
        border.left: 16
        border.right: 16
        border.top: 12
        border.bottom: 20
        source: "qrc:/images/DialogFrame3.png"
    }
    onClosed:{
        pop.destroy()
    }

    Component.onCompleted: {
        loadData()
    }

    function loadData()
    {
        var request = HttpHelper.createIMRequest();
//        request.enabledLog=true
        request.onSucceed( function(request){
            console.log('----water_device_get_orderinfo---',JSON.stringify(request.responseData))
            orderInfo=request.responseData
        } );
        request.onError(  function(request){
            pop.close()
            CGlobal.showMessage(window,request.errorMessage)
        } );
        request.get("/sys/watpc/pc/water_device_get_orderinfo/"+bno)
    }

    function payCallBack(oid){
        console.log('---------pay successed------',oid)
        goodData.clear()
        pop.close()
    }

    contentItem: Item {
        anchors.fill: parent
        anchors.margins: 20
        Item {
            anchors.fill: parent

            Item {
                width: parent.width
                height: 120
                Column{
                    spacing: 6
                    leftPadding: 30
                    Row{
                        Item {
                            width: 200
                            height: 26
                            Text {
                                color: '#333333'
                                text: qsTr("用户昵称: ")
                                anchors.verticalCenter: parent.verticalCenter
                                Text {
                                    color: '#333333'
                                    text: {
                                        if(orderInfo && 'w_nickname' in orderInfo){
                                            return orderInfo.w_nickname
                                        }
                                        return ''
                                    }
                                    anchors.verticalCenter: parent.verticalCenter
                                    anchors.left: parent.right
                                }
                            }
                        }
                        Item {
                            width: 200
                            height: 26
                            Text {
                                color: '#333333'
                                text: qsTr("订单编号: ")
                                anchors.verticalCenter: parent.verticalCenter
                                Text {
                                    color: '#333333'
                                    text: {
                                        if(orderInfo && 'w_no' in orderInfo){
                                            return orderInfo.w_no
                                        }
                                        return ''
                                    }
                                    anchors.verticalCenter: parent.verticalCenter
                                    anchors.left: parent.right
                                }
                            }
                        }
                    }
                    Row{
                        Item {
                            width: 200
                            height: 24
                            Text {
                                color: '#333333'
                                text: qsTr("手机号: ")
                                anchors.verticalCenter: parent.verticalCenter
                                Text {
                                    color: '#333333'
                                    text: {
                                        if(orderInfo && 'w_phone' in orderInfo){
                                            return orderInfo.w_phone
                                        }
                                        return ''
                                    }
                                    anchors.verticalCenter: parent.verticalCenter
                                    anchors.left: parent.right
                                }
                            }
                        }
                        Item {
                            width: 200
                            height: 24
                            Text {
                                color: '#333333'
                                text: qsTr("下单时间: ")
                                anchors.verticalCenter: parent.verticalCenter
                                Text {
                                    color: '#333333'
                                    text: {
                                        if(orderInfo && 'create_time' in orderInfo){
                                            return orderInfo.create_time
                                        }
                                        return ''
                                    }
                                    anchors.verticalCenter: parent.verticalCenter
                                    anchors.left: parent.right
                                }
                            }
                        }
                    }
                    Row{
                        Item {
                            width: 200
                            height: 24
                            Text {
                                color: '#333333'
                                text: qsTr("手环号: ")
                                anchors.verticalCenter: parent.verticalCenter
                                Text {
                                    color: '#333333'
                                    text: {
                                        if(orderInfo && 'b_name' in orderInfo){
                                            return orderInfo.b_name
                                        }
                                        return ''
                                    }
                                    anchors.verticalCenter: parent.verticalCenter
                                    anchors.left: parent.right
                                }
                            }
                        }
                        Item {
                            width: 200
                            height: 26
                            Text {
                                color: '#333333'
                                text: qsTr("缴纳押金: ")
                                anchors.verticalCenter: parent.verticalCenter
                                Text {
                                    color: '#333333'
                                    text: {
                                        if(orderInfo && 'deposit' in orderInfo){
                                            return orderInfo.deposit
                                        }
                                        return ''
                                    }
                                    anchors.verticalCenter: parent.verticalCenter
                                    anchors.left: parent.right
                                }
                            }
                        }
                    }
                }
            }

            Rectangle{
                width: parent.width
                height: 1
                color: '#dddddd'
                y:110
            }

            Text {
                y:130
                color:'black'
                text: returnGoods ? qsTr("退货商品") : qsTr("购买商品")
                x:30
            }
            Flickable{
                id:flick
                x:30
                y:160
                width: 500
                height: parent.height - y - 120
                contentWidth: width
                contentHeight: col.height
                ScrollBar.vertical: ScrollBar{}
                clip: true
                boundsBehavior: Flickable.StopAtBounds
                Column{
                    id:col
                    spacing: 10
                    Repeater{
                        model: goodData
                        Item {
                            width:flick.width
                            height: 24
                            property var item: goodData.get(index)
                            Text {
                                anchors.verticalCenter: parent.verticalCenter
                                color: '#333333'
                                text: {
                                    if(parent.item)
                                    {
                                        return parent.item.name +' '+ parent.item.specs +' x'+parent.item.count
                                    }
                                    return ''
                                }
                            }
                            Text {
                                anchors.verticalCenter: parent.verticalCenter
                                color: 'red'
                                text:
                                {
                                    if(parent.item)
                                    {
                                        return '￥'+(parent.item.count * parent.item.price).toFixed(2)
                                    }
                                    return ''
                                }
                                anchors.right: parent.right
                                anchors.rightMargin: 0
                            }
                        }
                    }
                }
            }


            Item {
                width: parent.width
                height: 100
                anchors.bottom: parent.bottom
                Rectangle{
                    width: parent.width
                    height: 1
                    color: '#dddddd'
                }

                Text {
                    font.pixelSize: 24
                    color: 'red'
                    x:20
                    y:20
                    text:{
                        let all=0
                        for(let i=0;i<goodData.count;i++)
                        {
                            let item=goodData.get(i)
                            all += (item.count * item.price)
                        }
                        return qsTr("合计: ￥")+all.toFixed(2)+qsTr('元')
                    }
                }

                Button{
                    width: 120
                    height: 40
                    anchors.right: parent.right
                    anchors.rightMargin: 10
                    anchors.bottom: parent.bottom
                    background: Rectangle{
                        radius: 10
                        color: '#319B0B'
                        Text {
                            anchors.centerIn: parent
                            color: '#ffffff'
                            text: returnGoods ? qsTr("确定退货") : qsTr("确定售卖")
                        }
                    }
                    onClicked: {

                        if(!orderInfo)
                        {
                            return
                        }

                        if(returnGoods)
                        {
                            //退货接口
                            var request = HttpHelper.createIMRequest();
                           // request.enabledLog=true
                            request.onSucceed( function(request){
                                goodData.clear()
                                pop.close()
                                CGlobal.showMessage(window,qsTr('退货成功'))
                            } );
                            request.onError(  function(request){
                                CGlobal.showMessage(window,request.errorMessage)
                            } );
                            let ids=[]
                            for(let i=0;i<goodData.count;i++)
                            {
                                let item=goodData.get(i)
                                ids.push({id:item.id,num:item.count})
                            }
                            request.post("/sys/watpc/pc/water_product_refund",
                                         {
                                             b_id:orderInfo.id,
                                             product:ids
                                         })
                        }
                        else
                        {
                            //售货下单接口
                            var request = HttpHelper.createIMRequest();
                            // request.enabledLog=true
                            request.onSucceed( function(request){
                                //                                 CGlobal.createObjectAndOpen(window,'qrc:/main/mamager/settlement/PayDialog.qml',
                                //                                                             {
                                //                                                                 amount:request.responseData.amount,
                                //                                                                 bno:request.responseData.sno,
                                //                                                                 callBackFunc:payCallBack
                                //                                                             })
                                goodData.clear()
                                pop.close()
                                CGlobal.showMessage(window,qsTr('下单成功'))

                                //                                console.log('----water_product_confirm---',JSON.stringify(request.responseData))
                            } );
                            request.onError(  function(request){
                                CGlobal.showMessage(window,request.errorMessage)
                            } );
                            let ids=[]
                            for(let i=0;i<goodData.count;i++)
                            {
                                let item=goodData.get(i)
                                ids.push({id:item.id,num:item.count})
                            }
                            request.post("/sys/watpc/pc/water_product_confirm",
                                         {
                                             bno:bno,
                                             product:ids
                                         })
                        }

                    }
                }
            }
        }
//        Rectangle{
//            width: 140
//            height: 80
//            border.color: '#dddddd'
//            radius: 10
//            anchors.right: parent.right
//            anchors.rightMargin: 50
//            Image {
//                id:imgSh
//                anchors.centerIn: parent
//                sourceSize: Qt.size(60, 60)
//                source: "qrc:/images/main/shouhuan1.png"
//                visible: true
//            }
//        }
        Button{
            width: 32
            height: 32
            anchors.right: parent.right
            background: Image {
                anchors.centerIn: parent
                source: "qrc:/images/close.png"
            }
            onClicked: {
                pop.close()
            }
        }
    }
}
